<template>
  <div class="loginBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{formTitle}}</span>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          >
            <svg-icon slot="suffix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
          >
            <svg-icon slot="suffix" :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" class="el-input__icon input-icon" @click="showPwd" />
            <svg-icon slot="suffix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="loginForm.captcha" name="captcha" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin">
            <svg-icon slot="suffix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode">
          </div>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;background-color:#007AFF" @click.native.prevent="handleLogin">登录</el-button>
      </el-form>

    </el-card>
  </div>

</template>

<script>
import defaultSetting from '@/settings.js'
import { getCodeImg } from '@/api/login'
import { getUUID } from '@/utils'
import md5 from 'js-md5';
export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      formTitle: defaultSetting.title,
      loginForm: {
        username: '',
        password: '',
        rememmberMe: false,
        captcha: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        captcha: [{ required: true, trigger: 'blur', message: '验证码不能为空' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // 获取验证码
    this.getCode()
  },
  methods: {
    getCode() {
      const uuid = getUUID();
      getCodeImg(uuid).then(res => {
        const { data } = res;
        this.codeUrl = data.base64Str;
        this.loginForm.uuid = uuid;
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    initPassword() {
      this.loginForm.password = ''
      this.loginForm.captcha = ''
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.loginForm.password = md5(this.loginForm.password)
          this.$store.dispatch('Login', this.loginForm).then(() => {
            // this.$router.push({ path: this.redirect || '/' })
            this.$router.push({ path: '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.getCode()
            this.initPassword()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.loginBox {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 200px;
}
.box-card {
  width: 350px;
}
.login-code {
  width: 33%;
  display: inline-block;
  height: 38px;
  float: right;
  img{
    max-width:100%;
    height:100%;
  }
}
</style>
